﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.BLL.Project;
using Jzy.Core.Common;
using Jzy.Model.Project;
using System.Data;


using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

namespace Jzy.Web.Project.BugManage
{
    public partial class PrjBugList : PageList
    {
        private PrjBugBO _bug;
        private PrjBugBO Bug
        {
            get
            {
                if (_bug == null)
                {
                    _bug = new PrjBugBO();
                }
                return _bug;
            }
        }

        private int Status
        {
            get
            {

                //如果非返回，则清空session
                if (!IsReturn && !IsPostBack)
                {
                    Session["BugStatus"] = null;
                }

                if (Session["BugStatus"] == null)
                {
                    Session["BugStatus"] = -1;
                }

                return (int)Session["BugStatus"];
            }
            set
            {
                Session["BugStatus"] = value;
            }
        }

        /// <summary>
        /// 用于记录当前的页数
        /// </summary>
        private new object PageIndex
        {
            get
            {
                //如果非返回，则清空session
                if (!IsReturn && !IsPostBack)
                {
                    Session["PageIndex"] = null;
                }

                return Session["PageIndex"];
            }
            set
            {
                Session["PageIndex"] = value;
            }
        }

        /// <summary>
        /// 是否通过返回按钮过来的
        /// </summary>
        private bool IsReturn
        {
            get
            {
                if (string.IsNullOrEmpty("IsReturn"))
                {
                    return false;
                }
                else
                {
                    try
                    {
                        return Convert.ToBoolean(Request["IsReturn"]);
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
        }
        private PagerSortSetting _setting;

        private string PrjID
        {
            get
            {
                return Decrypt(Request.QueryString["PrjID"]);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            _setting = this.Pager1.InitPager(new BindViewHandler(BindView));
            if (!IsPostBack)
            {
                if (PageIndex != null)
                {
                    _setting.PageIndex = (int)PageIndex;
                }
                BindStatus();
                BindView();
            }
            JSUtil.SetTableHeight("list", 100);
        }
        private void BindStatus()
        {
            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(BugStatus));

            EnumInfo info = new EnumInfo();
            info.ID = -1;
            info.Name = "全部";
            list.Insert(0, info);

            rptStatus.DataSource = list;
            rptStatus.DataBind();
        }
        protected void rptStatus_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Status = Convert.ToInt16(e.CommandArgument);
           
            BindView();
        }
        private void BindView()
        {
            if (Status != -1)
            {
                lblStatus.Text = ((BugStatus)Status).ToString();
            }

            lv.DataKeyNames = new string[] { "ID", "Status" };
            InitLV(lv, lvHeader);
            // lv.EnableViewState = true;

            if (Status == -1)
            {
                pnlStatus.Visible = false;
            }
            else
            {
                pnlStatus.Visible = true;
            }

            List<PrjBugInfo> list = Bug.GetListByProject(PrjID, Status);


            lv.DataSource = _setting.GetList<PrjBugInfo>(list);
            lv.DataBind();

            PageIndex = _setting.PageIndex;
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                ListViewDataItem item = (ListViewDataItem)e.Item;
                string strEncryptID = StrUtil.EncryptUrl(lv.DataKeys[item.DataItemIndex]["ID"].ToString());

                HyperLink lnkName = (HyperLink)e.Item.FindControl("lnkName");
                HyperLink lnkDetail = (HyperLink)e.Item.FindControl("lnkDetail");
                lnkName.NavigateUrl = lnkDetail.NavigateUrl = "Detail.aspx?FromPage=" + (int)FromPage.PrjBugList + "&ID=" + strEncryptID;
                BugStatus status = (BugStatus)(int)lv.DataKeys[((ListViewDataItem)e.Item).DataItemIndex]["Status"];
                Label lblStatus = (Label)e.Item.FindControl("lblStatus");

                Bug.SetColorByStatus(lblStatus, status);

            }
        }
       
    }
}
